<template>
    <div class="a1">
        <div class="login">
           <div class="left">
           <h1>高效记录生活</h1>
           <p>轻松收集，有效整理</p>
                <div class="le"></div>
           </div>
           <div class="right">
                <div class="bg">
                     <div class="a2">
                     <img src="../assets/logo.png" alt="" class="img">
                        <p class="p1">欢迎登录</p>
                        <div class="as">
                            <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" status-icon
                                :rules="rules" label-width="auto" class="demo-ruleForm">
                                <el-form-item label="账号" prop="username">
                                    <el-input v-model="ruleForm.username" type="username" autocomplete="off" />
                                </el-form-item>
                                <el-form-item label="密码" prop="password">
                                    <el-input v-model="ruleForm.password" type="password" autocomplete="off" />
                                </el-form-item>
                                <el-form-item>
                                    <el-button @click="resetForm(ruleFormRef)">重置</el-button>
                                    <el-button type="primary" @click="submitForm(ruleFormRef)">
                                        登陆
                                    </el-button>
                                </el-form-item>
                            </el-form>
                            
                        </div>
                     </div>
                </div>
           </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import router from '@/router';
import { ElMessage } from 'element-plus'
import { gologin } from '@/api/login';

const ruleFormRef = ref<FormInstance>()

const validatorFn = (rule: any, value: any, callback: any) => {
    const zz = /^\d{5,8}$/
    if (zz.test(value)) {
        callback()
    } else {
        callback(new Error('密码格式为5~8位'))
    }
}

const ruleForm = reactive({
    username: '',
    password: '',
})

const rules = reactive<FormRules<typeof ruleForm>>({
    username: [{ trigger: 'blur', required: true, message: '请输入账号' }],
    password: [{ trigger: 'blur', required: true, message: '请输入密码' }, {
        validator: validatorFn
    }]
})

const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate(async (valid) => {
        if (valid) {
            const res=await gologin(ruleForm)
            console.log(res);
            if(res.data.code===200){
                console.log('登陆!')
                router.push('/')
                console.log(res);
                
                localStorage.setItem('token',res.data.data.token)
                localStorage.setItem('obj',JSON.stringify(res.data.data.obj))
            }
            

        } else {
            console.log('error submit!')
        }
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}

</script>

<style scoped>
.a1{
    width: 100%;
    height: 96%;
    background: url("../assets/bg.png");
    display: flex;
	align-items: center;
	justify-content: center;
}
.login{
    width: 90%;
    height: 90%;
    background: hsla(0, 0%, 100%, 0.85);
    /* display: flex;
	align-items: center;
	justify-content: center; */
}
.left{
    margin-top: 10%;
    width: 45%;
    height: 80%;
    margin-left: 5%;
    float: left;
}
.le{
    width: 700px;
    height: 450px;
    background: url("../assets/login.png");
}
.right{
    width: 33%;
    height: 500px;
    float: right;
}
.bg{
    width: 90%;
    height: 50%;
    margin-top: 50%;
    box-shadow: 2px 3px 7px rgb(0 0 0 / 20%);
}
.a2{
    width: 100%;
    height: 100%;
}
.img{
    width: 70px;
    height: 50px;
    float: left;
    margin-left: 18%;
   
}
.p1{
    font-size: 30px;
    font-weight: bold;
    color: #2d51e6;
}
.as{
    margin-top: 10px;
}

</style>